package com.oasistudio.tk.connector.imp;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.oasistudio.tk.connector.TopAPIConnectorHelper;
import com.oasistudio.tk.connector.TopApiRequest;
import com.oasistudio.tk.connector.TopApiResponse;
import com.taobao.tbw.common.TBWContext;

/**
 * 获取淘宝画报的所有频道
 */
public class TaobaoPosterChannelsGetConnector implements TopAPIConnectorHelper {
	
	public TaobaoPosterChannelsGetConnector() {
	}

	@Override
	public Object asyncPaser(byte[] part) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getApiUrl() {
		TopApiRequest request = new TopApiRequest();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		TreeMap<String, String> params = new TreeMap<String, String>();
		params.put("method", "taobao.poster.channels.get");
		params.put("session", TBWContext.getInstance().getTopsession());
		params.put("timestamp", sdf.format(calendar.getTime()));
		params.put("format", "json");
		params.put("app_key", TBWContext.getInstance().getAppKey());
		params.put("v", "2.0");
		params.put("partner_id", "top-apitools");
		params.put("sign_method", "md5");
		//API method 参数
		request.params = params;
		String url = request.generalRequestUrl(true);
		System.out.println("ItemListConnector.getApiUrl() url = "+url);
		return url;
	}

	/**
	 * 
	 */
	@Override
	public Object syncPaser(byte[] all) {
		ChannelsInfo channelsInfo = new ChannelsInfo();
		try {
			TopApiResponse response = new TopApiResponse("poster_channels_get_response");
			String str = new String(all, "UTF-8");
			//Log.d("return msg:", str);
			if (response.parseResult(str).success) {
				channelsInfo.isSuccess = true;
				JSONObject jsObj = response.data;
				if (jsObj.has("channels")) {
					JSONObject itemsJSON = jsObj.getJSONObject("channels");
					if (itemsJSON.has("huabao_channel")) {
						JSONArray channelsArray = itemsJSON.getJSONArray("huabao_channel");
						Channel[] channels = new Channel[channelsArray.length()];
						channelsInfo.channels = channels;
						for (int i = 0; i < channelsArray.length(); i++) {
							JSONObject channelJSON = channelsArray.getJSONObject(i);
							channels[i] = new Channel();
							channels[i].id = channelJSON.optLong("id");
							channels[i].channel_name = channelJSON.optString("channel_name");
							channels[i].name_en = channelJSON.optString("name_en");
							channels[i].description = channelJSON.optString("description");
							channels[i].channel_url = channelJSON.optString("channel_url");
						}
					}
				}
			} else {
				channelsInfo.isSuccess = false;
				channelsInfo.errCode = response.errCode;
				channelsInfo.errInfo = response.errInfo;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return channelsInfo;
	}

	public static class ChannelsInfo {
		public String errCode;
		public String errInfo;
		public Boolean isSuccess;
		public Channel[] channels;
	}

	public static class Channel {
		public String channel_name;//
		public Number id;//channel id
		public String name_en;//
		public String description;//
		public String channel_url;	
		public String toString(){
			StringBuilder sb = new StringBuilder();
			sb.append("id:"+id);
			sb.append(";channel_name:"+channel_name);
			sb.append(";name_en:"+name_en);
			sb.append(";description:"+description);
			return sb.toString();
		}
	}
	
	public static class Location{
		public String state;
		public String city;
		
		public String toString(){
			return state+" "+city;
		}
	}
}
